#include "MoveCockroach.h"
#include "HelloWorldScene.h"
#include "Cockroach.h"
#include "cocos2d.h"
USING_NS_CC;

int lan_chay;

MoveCockroach::MoveCockroach()
{
    setName("Move");
    /*curAction = NULL;
	sizeScreen = CCDirector::sharedDirector()->getWinSize();
    m_move_up_count = 0;
	m_move1_left = - 1;
	lan_chay = 0;*/
}


// Update trang thai cho con gian + goi cac ham callback o day
void MoveCockroach::update(float delta)
{
	parent = (CockRoach*)getOwner();

	if (parent == NULL) return;

	if(parent->getPositionY() < 0)
	{
		parent->stopAllActions();

		parent->update(delta);
	}

	//oldpos = parent->getPosition();

	//CCLog("%f", parent->getPositionX());

 //   if (parent == NULL) return;
	//
	//if(parent->getStatus() == ESCAPE)
	//{
	//	CCLog("++++++Tron thoat");
	//	parent->updateEscape(); // Cap nhat so gian tron thoat
	//	actionStop(); // Stop all action
 //       return;
	//
	//}

	//// DEATH CALL BACK, TRON THOAT CUNG HET PHIM
 //   if (parent->getStatus() == DEATH) // Chet la het PHIM
 //   {
	//	actionStop(); // Stop all action
 //       return;
 //   }
	//// RUN callback
 //   else if (parent->getStatus() == RUN)
 //   {
 //       actionRun();//nhay len va roi xuong
 //   }
	//else if (parent->getStatus() == RUNNING)
	//{
	//	return;
	//}
	//// IDLE callback funtion
	//else if (parent->getStatus() == IDLE)
 //   {
 //       actionIdle(); //cam dau xuong dat...
 //   }

	

}

// Ham di chuyen cua con gian
// Con gian se chay tu tren xuong duoi voi toc do tang theo level
void MoveCockroach::actionRun()
{
    //truoc khi nhay len => stop action truoc...
	if(curAction)
	{
		actionStop();
	}
   
	// Check so gian tau thoat
	if(parent->getPositionY() <= 1 )
	{
		parent->setStatus(ESCAPE);
	}

	// srand( time(NULL)*500);
	// Huong con gian se xoay o lan ke tiep
	//int direction = rand()% 8 + 1; // Bon phuong tam huong

	//int p = rand() % (90000 - 1 + 1) + 1;
	//int direction = (p / 30000) - 1;
	// float random = CCRANDOM_0_1() * 5; //[0,5] CCRANDOM_0_1() Value range[0,1]
	//int direction = rand() % (1 - (-1) + 1) + (-1);
	//int random = CCRANDOM_0_1() * 100;
	//CCLog("P random = %d",p);
	//CCLog("P / 300 = %d",random / 3000);
	//int direction = random / 30;
	//CCLog("Huong chay = %d",direction);

	// Khoang cach mo lan di chuyen (quang duong)
	int distance = 10;
	// So lan gian di chuyen len tren
	// move_up_count = 0;

	// Up position
	CCPoint up_pos = parent->getPosition();

	fMove2(up_pos);

	// Down position
	/*float down_pos_y = up_pos.y - distance;
	float down_pos_x;
	if(direction==0){
		down_pos_x = up_pos.x;
	}else if(direction==1){
		down_pos_x = up_pos.x - distance;
	}else if(direction== -1){
		down_pos_x = up_pos.x + distance;
	}
	CCPoint down_pos = CCPoint::CCPoint(ccp(down_pos_x,down_pos_y));*/
	// rand() % (max - min + 1) + min;

	
	// Next position
	/*float next_pos_x;
	float next_pos_y;
	int rands = rand() % (1 - 0 + 1) + 0;
	CCLog("Rand  = %d",rands);
	CCLog("Move up count %d",m_move_up_count);
	if(rands == 0 && m_move_up_count <=2){
		next_pos_y = down_pos_y + distance;
		m_move_up_count ++;
	}else{
		next_pos_y = down_pos_y - distance;
	}

	int rands2 = rand() % (1 - (-1) + 1) + (-1);
	CCLog("Rand 2 = %d",rands2);
	if(rands2==0){
		next_pos_x = down_pos.x;
	}else if(rands2==1){
		next_pos_x = down_pos.x + distance;
	}else if(rands2==-1){
		next_pos_x = down_pos.x - distance;
	}*/


	//CCPoint next_pos = CCPoint::CCPoint(ccp(next_pos_x,next_pos_y));

	// Speed with Level
	//__speed = getDuraRun(1);
	//__speed = 50.0;


	//CCMoveTo * move = CCMoveTo::create(__speed,ccp(newPosX,newPosY));
	//CCPoint moveDifference = ccpSub(currentPosition, __move);
	//float distanceToMove = ccpLength(moveDifference);
	//float moveDuration = distanceToMove / __speed;
	//CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	//if(next_pos.x >= visibleSize.width && next_pos.y >= visibleSize.height){
		//fMoveTo(next_pos);
	//}

}

void MoveCockroach::fMoveTo(CCPoint p)
{
	float __speed = 1.2f;
	actionMove = CCMoveTo::create(__speed ,p);
	parent->runAction(CCSequence::createWithTwoActions(actionMove , CCCallFuncO::create(actionMove , callfuncO_selector(MoveCockroach::fCallbacktemp) , (CCObject *) parent)));
}

void MoveCockroach::fCallbacktemp(CCObject * object){
	//CCLOG("CALL back");
	((CockRoach * ) object)->setStatus(RUN);
}

void MoveCockroach::fMove1(CCPoint p)
{
	lan_chay ++ ;
	//CCLOG(" %d" ,  lan_chay);
	parent->setStatus(RUNNING);
	/*parent->setStatus(RUNNING);
	
	float start_X = p.x;
	float start_Y = p.y;

	float min_X = p.x ;
	float min_Y = p.y - 30;

	CCLOG("%f %f %d" , min_X , min_Y , lan_chay);

	fMoveTo(ccp(min_X , min_Y));*/
	if(m_move1_left == -1)
	{
		fMoveTo(ccp(p.x -30 , p.y -30));
		m_move1_left = 1;
		//CCLog("Move to left");

	} else {
		fMoveTo(ccp(p.x +30 , p.y -30));
		
		m_move1_left = -1;
		//CCLog("Move to right");
	}
	
}

/// 
/// CHU THICH SO LUOC VE HAM fMove2
/// p: toa do o thoi diem hien tai cua con gia
/// 
/// Gian di chuyen theo duong thang
/// 
///
/// @author : Quach Van Quyet
/// time_create: 18/6/2014
/// time_end: 18/6/2014
///
void MoveCockroach::fMove2(CCPoint p)
{
	lan_chay ++ ;
	//CCLOG(" %d" ,  lan_chay);
	parent->setStatus(RUNNING);
	fMoveTo(ccp(p.x , p.y -30));
	
}


void MoveCockroach::actionIdle()
{
	//CCLog("Trang thai bat dau cua con gian");
	// Hieu ung faceIn luc con gian xuat hien
	//parent->fCockRoachAppear();

}

void MoveCockroach::rotate(float t)
{
	//parent = (CockRoach*)getOwner();

	CCPoint newpos = parent->getPosition();

	CCPoint pos1 = oldpos;

	CCPoint pos2 = newpos;

	float theta = atan((pos1.y-pos2.y)/(pos1.x-pos2.x)) * 180 * 7 /22; // 22/7 = pi

	float calculatedAngle = 0;

	if(pos1.y - pos2.y > 0)
	{
		if(pos1.x - pos2.x < 0)
		{
			calculatedAngle = (-90-theta);
		}
		else if(pos1.x - pos2.x > 0)
		{
			calculatedAngle = (90-theta);
		}       
	}
	else if(pos1.y - pos2.y < 0)
	{
		if(pos1.x - pos2.x < 0)
		{
			calculatedAngle = (270-theta);
		}
		else if(pos1.x - pos2.x > 0)
		{
			calculatedAngle = (90-theta);
		}
	}

	if (calculatedAngle < 0)
	{
		calculatedAngle+=360;
	}
	calculatedAngle += 180;

	//CCLog("angle %f", calculatedAngle);

	parent->runAction(CCRotateTo::create(0.05f, calculatedAngle));

	oldpos = newpos;
}


// Action Death callback -> thuc hien viec thay the anh va tao ra cac hieu ung chet
// Phun mau
void MoveCockroach::actionDeath()
{
    CCPoint upPos = parent->getPosition();

	parent->setStatus(IDLE); // chet -> song lai va chay tiep cho den khi ket thuc game
	parent->fUpdateDieImage();
    parent->stopAllActions();
	
}

void MoveCockroach::actionStop()
{
    if (curAction != NULL)
    {
        parent->stopAction(curAction);
        curAction = NULL;

		// display score table


    }
}

// Extras function here
float MoveCockroach::getDuraRun(int level)
{
	if(level < 1)
	{
		return 0.6f;
	}
	if(level ==1)
	{
		return 0.5f;
	}
	else if(level ==2)
	{
		return 0.4f;
	}
	else if(level ==3)
	{
		return 0.35f;
	}
	else if(level ==4)
	{
		return 0.3f;
	}
	else if(level == 5)
	{
		return 0.2f;
	}
	else
	{
		return 0.2f;
	}
}